/**
 *
 */
package cn.edu.cqupt.cyExam.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;


import cn.edu.cqupt.cyExam.myBatis.bean.User;
import cn.edu.cqupt.cyExam.service.UserService;
import cn.edu.cqupt.cyExam.utils.JSONResult;
import cn.edu.cqupt.cyExam.utils.Utils;


@RestController
@CrossOrigin
public class UserController
{
	@Autowired
	private JavaMailSender mailSender;
	@Resource
	private UserService  userService;


	@RequestMapping("/Register")

	public JSONResult register(@RequestParam(value="register_username",required=false) String username,
							   @RequestParam(value="register_password",required=false) String password,
							   @RequestParam(value="register_email",required=false) String email,
							   HttpSession session,
							   HttpServletRequest request) {


		String userPasswordMD5=Utils.getMD5(password);
		int CheckAcount=userService.CheckAcount(username);
		if(CheckAcount==1) {
			 return new JSONResult(1, "注册失败:账号已经存在", 0, null);
		}

		int CheckEmail=userService.CheckEmail(email);
		if(CheckEmail != 0) {
			 return new JSONResult(1, "注册失败:邮箱已经存在", 0, null);
		}
		User user = new User();
		user.setUser_name(username);
		user.setUser_password(userPasswordMD5);
		user.setUser_email(email);
		user.setUser_postion(0);
		 int intUser=userService.InsertUsers(user);
		 if(intUser==1) {

			 return new JSONResult(0, "注册成功", 1, null);
		 }else {
			 return new JSONResult(1, "注册失败", 0, null);
		 }

	}

	@GetMapping("/ToLogin")
	public JSONResult toLogin()
	{
		 return new JSONResult(3, "请登录后查看！", 0, null);
	}

	@GetMapping("/CheckStaff")
	public JSONResult checkStaff()
	{
		 return new JSONResult(5, "你未拥有此权限！", 0, null);
	}

	@RequestMapping("/LoginCheck")
	public JSONResult loginCheck(@RequestParam(value="username",required=false) String username,
								 @RequestParam(value="password",required=false) String password,
								 HttpSession session,
								 HttpServletRequest request) {
		User user=userService.getUsersAccountnumber(username);
		System.out.print(user);
		String userPasswordMD5=Utils.getMD5(password);
		System.out.print(userPasswordMD5);
		if(user !=null&&user.getUser_password().equalsIgnoreCase(userPasswordMD5)) {

			request.getServletContext().setAttribute(username, user);
			request.getServletContext().setAttribute("usersPosition", user.getUser_postion());
			 return new JSONResult(0, "登录成功", 1, user);




		}
		 return new JSONResult(1, "登录失败:请检查账号或者密码", 0, user);
	}

	@RequestMapping("/DeleteUser")
	public JSONResult deleteUser(@RequestParam(value="username",required=false) String username,
								 HttpSession session,
								 HttpServletRequest request) {
		System.out.print(username);
		System.out.print("_____lining_____");
		User user=userService.getUsersAccountnumber(username);
		if(user != null){
			int deleteUser = userService.deleteUser(user.getUser_name());
			System.out.print("------------------------");
			System.out.print(deleteUser);
			return new JSONResult(0,"删除成功",1,null);

		}
		return new JSONResult(1,"该用户不存在",1,null);


	}

	@PostMapping("/ToSendMail")
	public JSONResult toSendMail(@RequestParam("email")String email,@RequestParam("account")String account) {
		User user=userService.getUsersAccountnumber(account);
		String ToEmail=user.getUser_name();
		String subject="我们已经重置您的密码，请用以下密码登录，切勿给他人泄露";
		int Mailpassword=(int) (1000000000+(Math.random()*1000000000));


		String Mailpasswordtext=Mailpassword+"";

		String MailPasswordByMD5=Utils.getMD5(Mailpasswordtext);
		userService.updatepasswordByeMail(MailPasswordByMD5,account);
		try {
			SimpleMailMessage message = new SimpleMailMessage();
			message.setFrom("1252582408@qq.com");
			message.setTo(email);
			message.setSubject(subject);
			message.setText("重置密码为："+Mailpasswordtext+"------------发送至："+ToEmail);

			mailSender.send(message);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return new JSONResult(0, "重置成功,请查看邮件", 0, null);
	}

}


